import { ref, reactive, onMounted, watch, inject } from 'vue'
import { addOrganizerRemarktApi } from '@/api/manage'
import { FuncList } from '@/type/BaseType'
import TipDialog from '@/components/Dialog/index.vue'
import { Toast } from '@nutui/nutui'
export default function useAddress(data?: any, flag?: boolean) {
	//新增弹框的ref属性

	const tipDialogRef = ref<InstanceType<typeof TipDialog>>()

	const ruleForm = ref<any>(null)
	const listData = ref<any>()
	const rules = {
		organizer_remark: [{ required: true, message: '请输入备注' }]
	}
	const formData = reactive<any>({
		organizer_remark: undefined
	})

	const editMarkBtn = (n: any) => {
		listData.value = n
		formData.organizer_remark = undefined
		tipDialogRef.value?.openDialog()
		formData.organizer_remark = n.organizer_remark
	}
	const handleConfirm = (n: any) => {
		ruleForm.value.validate().then(async ({ valid, errors }: any) => {
			if (valid) {
				let res = await addOrganizerRemarktApi({
					order_id: listData.value.order_id,
					organizer_remark: formData.organizer_remark
				})
				if (res && res.code == 0) {
					Toast.success('编辑成功')
					tipDialogRef.value?.closeDialog()
					if (flag) {
						listData.value.organizer_remark = formData.organizer_remark
						return
					}
					data.list.map((item: any) => {
						if (item.order_id == listData.value.order_id) {
							item.organizer_remark = formData.organizer_remark
						}
					})
				}
			} else {
				console.log('error submit!!', errors)
			}
		})
	}
	return {
		editMarkBtn,
		formData,
		rules,
		ruleForm,
		handleConfirm,
		tipDialogRef
	}
}
